package com.yupi.usercenter.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.usercenter.base.util.JsonUtils;
import com.yupi.usercenter.base.util.SnowflakeIdGenerator;
import com.yupi.usercenter.basic.enums.CategoryEnum;
import com.yupi.usercenter.mapper.MonitorWoMapper;
import com.yupi.usercenter.model.dto.*;
import com.yupi.usercenter.model.entity.MonitorWo;
import com.yupi.usercenter.model.vo.MonitorWoDetailVO;
import com.yupi.usercenter.model.vo.MonitorWoExcelVO;
import com.yupi.usercenter.model.vo.MonitorWoPageVO;
import com.yupi.usercenter.service.IMonitorWoService;
import com.yupi.usercenter.stream.MessageProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 云路供应链科技有限公司 版权所有 © Copyright 2020
 * <p>
 * 监控工单表 服务实现类
 *
 * @author jiweiyu
 * @version 1.0.0
 * @date Created in 2021-03-05
 */
@Slf4j
@Service
public class MonitorWoServiceImpl extends ServiceImpl<MonitorWoMapper, MonitorWo> implements IMonitorWoService {

    @Resource
    protected MessageProducer messageProducer;

    @Override
    public Page<MonitorWoPageVO> page(MonitorWoPageDTO dto) {
        log.info("监控工单查询，入参dto:{}", JsonUtils.toJson(dto));
            // 代理区查询
            return getmonitorWoPageVOPage(dto);

    }


    @Override
    public MonitorWoDetailVO detail(MonitorWoDetailDTO dto) {
        MonitorWo data = baseMapper.selectOne(new LambdaQueryWrapper<MonitorWo>().eq(MonitorWo::getId, dto.getId()));
        if (Objects.isNull(data)) {
            return null;
        }

        return getMonitorWoDetailVO(data);
    }


    @Override
    public Boolean add(MonitorWoAddDTO dto) {
        MonitorWo data = new MonitorWo();
        data.setId(SnowflakeIdGenerator.getId());
        data.setCreateBy(dto.getActionBy());
        data.setCreateByName(dto.getActionByName());
        data.setUpdateBy(dto.getActionBy());
        data.setUpdateByName(dto.getActionByName());
        boolean result = baseMapper.insert(data) == 1;

        log.info("监控工单表，新增:{} -> id:{}, param:{}", result, data.getId(), JSONUtil.toJsonStr(dto));
        return result;
    }

    @Override
    public Boolean edit(MonitorWoEditDTO dto) {
        MonitorWo data = new MonitorWo();
        data.setId(dto.getId());
        /*
         * TODO...
         */
        data.setUpdateBy(dto.getActionBy());
        data.setUpdateByName(dto.getActionByName());
        boolean result = baseMapper.updateById(data) == 1;

        log.info("监控工单表，修改:{} -> id:{}, param:{}", result, data.getId(), JSONUtil.toJsonStr(dto));
        return result;
    }

    @Override
    public Boolean delete(MonitorWoDeleteDTO dto) {
        boolean result = baseMapper.deleteById(dto.getId()) == 1;

        log.info("监控工单表，删除:{} -> id:{}, param:{}", result, dto.getId(), JSONUtil.toJsonStr(dto));
        return result;
    }

    @Override
    public Integer exportCount(MonitorWoExcelDTO dto) {
        // 总部导出
        if (CategoryEnum.NETWORK_TYPE_HEADQUARTERS.getId().equals(dto.getActionByNetworkId())) {
            return getHqProblemExpressWoExcelCount(dto);
        }
        // 代理区导出
        return getProblemExpressWoExcelCount(dto);
    }

    @Override
    public Page<MonitorWoExcelVO> export(MonitorWoExcelDTO dto) {

        // 总部导出
        if (CategoryEnum.NETWORK_TYPE_HEADQUARTERS.getId().equals(dto.getActionByNetworkId())) {
            return getHqProblemExpressWoExcelPage(dto);
        }

        // 代理区导出
        return getProblemExpressWoExcelPage(dto);
    }


    private MonitorWoDetailVO getMonitorWoDetailVO(MonitorWo data) {
        MonitorWoDetailVO monitorWoDetailVO = new MonitorWoDetailVO();
        BeanUtils.copyProperties(data, monitorWoDetailVO);
        return monitorWoDetailVO;
    }

    private List<Integer> getAllNextDetailById(Integer netWorkId) {
        // 获取申诉方以及对应的下级网点
        List<Integer> networkIds = new ArrayList<>();
        if (Objects.nonNull(netWorkId) && (!CategoryEnum.NETWORK_TYPE_HEADQUARTERS.getId().equals(netWorkId))) {
            networkIds.add(netWorkId);
        }
        return networkIds;
    }


    /**
     * @param dto
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.yl.qc.api.model.vo.monitorWoPageVO>
     * @author jiweiyu
     * @date 2020/11/5 10:32
     **/
    private Page<MonitorWoPageVO> getmonitorWoPageVOPage(MonitorWoPageDTO dto) {
        Page<MonitorWoPageVO> page = new Page<>(dto.getCurrent(), dto.getSize(), false);

        int count = baseMapper.selectPagesCount(dto);
        if (count == 0) {
            return page;
        }
        page.setTotal(count);
        List<MonitorWoPageVO> monitorWoPageVOS = baseMapper.selectPages(dto, (dto.getCurrent() - 1) * dto.getSize(), dto.getSize());
        page.setRecords(monitorWoPageVOS);
        return page;
    }


    /**
     * 总部查询问题件工单
     *
     * @param dto
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.yl.qc.api.model.vo.monitorWoPageVO>
     * @author jiweiyu
     * @date 2020/11/5 10:34
     **/
    private Integer getHqProblemExpressWoExcelCount(MonitorWoExcelDTO dto)  {
        return null;
    }

    private Integer getProblemExpressWoExcelCount(MonitorWoExcelDTO dto) {
        return null;
    }

    /**
     * @param dto
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.yl.qc.api.model.vo.monitorWoPageVO>
     * @author jiweiyu
     * @date 2020/11/5 10:32
     **/
    private Page<MonitorWoExcelVO> getHqProblemExpressWoExcelPage(MonitorWoExcelDTO dto) {
        return null;
    }

    private Page<MonitorWoExcelVO> getProblemExpressWoExcelPage(MonitorWoExcelDTO dto)  {
        return null;
    }


    @Override
    public Boolean sendMqMessage(List<MonitorWoAddDTO> list) {
        messageProducer.sendMonitorMessage(list);
        return null;
    }


}
